<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    protected $dates = ['created_at', 'updated_at', 'review_started_sms_notified_at', 'review_middle_sms_notified_at', 'review_ending_sms_notified_at'];

    protected $casts = [
        'is_applicant' => 'bool',
        'is_nominator' => 'bool',
        'is_reviewer' => 'bool',
        'is_meeting_reviewer' => 'bool',
        'is_committee' => 'bool',
        'is_admin' => 'bool',
        'is_tester' => 'bool',
        'is_staff'  => 'bool',
        'welcome_dialog_shown' => 'bool',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function isApplicant()
    {
        return $this->is_applicant;
    }

    public function isNominator()
    {
        return $this->is_nominator;
    }

    public function isReviewer()
    {
        return $this->is_reviewer;
    }

    public function isMeetingReviewer() {
        return $this->is_meeting_reviewer;
    }

    public function hasShownReviewerWelcomeDialog()
    {
        return $this->welcome_dialog_shown;
    }

    public function isCommittee()
    {
        return $this->is_committee;
    }

    public function isStaff() {
        return $this->is_staff;
    }

    public function isLargeGroupLeader()
    {
        return !empty($this->leading_large_group_id);
    }

    public function getLeadingLargeGroupName()
    {
        if (!$this->isLargeGroupLeader()) {
            return null;
        }
        $largeGroupNames = [1 => '路基路面组', 2 => '桥涵组', 3 => '隧道及地下工程组', 4 => '综合组', 5 => '科技创新组'];
        return $largeGroupNames[$this->leading_large_group_id];
    }

    public function isAdmin()
    {
        return $this->is_admin;
    }

    public function isTester()
    {
        return $this->is_tester;
    }

    public function isPersonalUser()
    {
        return $this->entity_type === 'personal';
    }

    public function isOrgUser()
    {
        return $this->entity_type === 'org';
    }

    public function isOrgCertified()
    {
        return $this->org_certify_status === 'certified';
    }

    public function isOrgCertifyFailed()
    {
        return $this->org_certify_status === 'failed';
    }

    public function getOrgCertifyStatus()
    {
        return $this->org_certify_status;
    }

    public function getRoleDisplayText()
    {
        $roleNames = [];
        if ($this->isApplicant()) {
            $roleNames[] = config('vars.role_names')['applicant'];
        }
        if ($this->isNominator()) {
            $roleNames[] = config('vars.role_names')['nominator'];
        }
        if ($this->isReviewer()) {
            $roleNames[] = config('vars.role_names')['reviewer'];
        }
        if ($this->isCommittee()) {
            $roleNames[] = config('vars.role_names')['committee'];
        }
        if ($this->isAdmin()) {
            $roleNames[] = config('vars.role_names')['admin'];
        }
        return implode('/', $roleNames);
    }
}
